package com.geese.module.business.controller;

import com.geese.common.BaseController;
import com.geese.common.entity.Business;
import com.geese.common.entity.Order;
import com.geese.common.entity.TransferRecordDO;
import com.geese.common.util.BeanUtil;
import com.geese.common.util.DateUtil;
import com.geese.common.util.TextValidator;
import com.geese.common.vo.Page;
import com.geese.exception.BusinessException;
import com.geese.module.business.dao.BusinessMapper;
import com.geese.module.business.dao.TransferRecordMapper;
import com.geese.module.business.dto.BusinessDTO;
import com.geese.module.business.service.IBusinessService;
import com.geese.module.business.vo.BusinessVO;
import com.geese.module.ceo.dao.OrderMapper;
import com.geese.module.ceo.service.OrderService;
import com.geese.module.user.model.SecurityUser;
import com.geese.module.user.service.UserService;
import com.geese.module.wechat.vo.ShopkeeperOrderVO;
import org.apache.commons.collections4.CollectionUtils;
import org.apache.commons.lang3.RandomUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.web.bind.annotation.*;

import java.util.Calendar;
import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * PC端商家
 * @author YeFangYi 
 */
@RestController
@RequestMapping(value = "/business/business")
@Scope("prototype")
public class BusinessController extends BaseController {

    @Autowired
    private OrderService orderService;
    @Autowired
    private UserService userService;
    @Autowired
    private IBusinessService businessService;

//    @ResponseBody
//    @GetMapping(value = "/orders/all")
//    public MyResult orders(Integer currPage, Integer pageSize, CeoModelVo ceoModelVo) {
//        currPage = currPage == null ? 1 : currPage;
//        pageSize = pageSize == null ? 20 : pageSize;
//        int type = paramTypeFromJwt();
//        if (type == 0) {
//            int id = paramIdFromJwt();
//            ceoModelVo.setBusinessId(id);
//            PageBean pageBean = orderService.getOrders(currPage, pageSize, ceoModelVo);
//            return MyResult.success(pageBean);
//        }
//        return MyResult.success();
//    }

    /**
     * 商家列表
     * @param token
     * @param businessVO
     * @return
     */
    @GetMapping("list")
    public String list(@RequestParam String token, BusinessVO businessVO) {
        userService.checkUser(token);
        Page page = getPageInfo(businessVO);
        Map<String, Object> result = businessService.selectBusinessList(page, businessVO);
        return response(result);
    }

    /**
     * 所有商家
     * @param token
     * @return
     */
    @GetMapping("/all")
    public String all(@RequestParam String token) {
//        userService.checkUser(token);
        Map<String, Object> result = businessService.selectAll();
        return response(result);
    }

    /**
     * 冻结单个
     * @param token
     * @param id
     * @return
     */
    @GetMapping("lock")
    public String lock(@RequestParam String token, @RequestParam int id){
        userService.checkUser(token);
        if (id <= 0) {
            throw new BusinessException(120009);
        }
        businessService.updateLock(id);
        return response();
    }

    /**
     * 冻结一群
     * @param token
     * @param ids
     * @return
     */
    @GetMapping("lockAll")
    public String lockAll(@RequestParam String token, @RequestParam List<Integer> ids){
        userService.checkUser(token);
        if (CollectionUtils.isEmpty(ids)) {
            throw new BusinessException(120009);
        }
        businessService.updateLockAll(ids);
        return response();

    }

    /**
     * 审核一个
     * @param token
     * @param business
     * @return
     */
    @PostMapping("audit")
    public String audit(@RequestParam String token, @RequestBody Business business){
//        userService.checkUser(token);
        if (business.getSettleCycle() == null || business.getSettleCycle() <= 0) {
            throw new BusinessException(120028);
        }
        if (business.getSingleAmount() == null || business.getSingleAmount() <= 0) {
            throw new BusinessException(120029);
        }
        if (business.getOrderAmount() == null || business.getOrderAmount() <= 0) {
            throw new BusinessException(120097);
        }
        if (business.getOrderAmount() > business.getSingleAmount()) {
            throw new BusinessException(120098);
        }
        if (business.getChannelId() == null || business.getChannelId() <= 0) {
            throw new BusinessException(120101);
        }
        if (business.getId() == null || business.getId() <= 0) {
            throw new BusinessException(120009);
        }
        business.setAudit(1);
        business.setAuditTime(new Date());
        businessService.updateAudit(business);
        return response();
    }

    /**
     * 审核一帮
     * @param token
     * @param ids
     * @return
     */
//    @GetMapping("auditAll")
    public String auditAll(@RequestParam String token, @RequestParam List<Integer> ids){
        userService.checkUser(token);
        if (CollectionUtils.isEmpty(ids)) {
            throw new BusinessException(120009);
        }
        businessService.updateAuditAll(ids);
        return response();
    }

    /**
     * 修改
     * @param token
     * @param business
     * @return
     */
    @PostMapping("edit")
    public String edit(@RequestParam String token, @RequestBody Business business) {
        userService.checkUser(token);
        if (business.getId() == null || business.getId() <= 0) {
            throw new BusinessException(120009);
        }
        if (StringUtils.isNotBlank(business.getMobile()) && !TextValidator.isMobileExact(business.getMobile())) {
            throw new BusinessException(120023);
        }
        businessService.updateBusiness(business);
        return response();
    }

    /**
     * 重置商家密码
     * @param token
     * @param id
     * @return
     */
    @PostMapping("reset")
    public String reset(@RequestParam String token, @RequestParam Integer id) {
        if (id == null || id <= 0) {
            throw new BusinessException(120009);
        }
        businessService.doReset(id);
        return response();
    }

    /**
     * 新增商家
     * @param token
     * @param businessDTO
     * @return
     */
//    @PostMapping("add")
    public String add(@RequestParam String token, @RequestBody BusinessDTO businessDTO) {
        if (StringUtils.isEmpty(businessDTO.getName())) {
            throw new BusinessException(120021);
        }
        if (StringUtils.isEmpty(businessDTO.getLicenseUrl())) {
            throw new BusinessException(120022);
        }
        if (StringUtils.isEmpty(businessDTO.getMobile()) || TextValidator.isMobileExact(businessDTO.getMobile())) {
            throw new BusinessException(120023);
        }
        if (StringUtils.isEmpty(businessDTO.getProvince())) {
            throw new BusinessException(120024);
        }
        if (StringUtils.isEmpty(businessDTO.getCity())) {
            throw new BusinessException(120025);
        }
        if (StringUtils.isEmpty(businessDTO.getArea())) {
            throw new BusinessException(120026);
        }
        if (StringUtils.isEmpty(businessDTO.getAddress())) {
            throw new BusinessException(120027);
        }
        if (StringUtils.isNumeric(businessDTO.getSettleCycle())) {
            throw new BusinessException(120028);
        }
        if (StringUtils.isNumeric(businessDTO.getSingleAmount())) {
            throw new BusinessException(120029);
        }
        if (StringUtils.isNumeric(businessDTO.getRemark())) {
            throw new BusinessException(120030);
        }
        SecurityUser user = userService.getLoginUser(token);
        Business business = BeanUtil.map(businessDTO, Business.class);
        business.setUserId(user.getUserId());
        businessService.addBusiness(business);
        return response();
    }
}
